import { HttpException, HttpStatus, Injectable } from "@nestjs/common";
import { EntityManager, getConnection, getManager, In } from "typeorm";
import { AccessDto } from "../../access/dto/access.dto";
import { Access } from "../../access/entities/access.entity";
import { RoleAccess } from "../../access/entities/role-access.entity";
import { AccessService } from "../../access/service/access.service";
import { RoleAccessData, RoleAccessDto } from "../dto/role-access.dto";
import { RoleAddAccessDto } from "../dto/role.dto";
import { Role } from "../entities/role.entity";

@Injectable()
export class RoleService {

    constructor(
        private readonly accessService: AccessService,

    ) { }

    async create(role: Role) {
        const u = await Role.findOne({ where: { roleName: role.roleName } });

        if (u) {
            throw new HttpException({
                message: 'Input data validation failed',
                error: 'roleName must be unique',
            },
                HttpStatus.BAD_REQUEST);
        }
        return await Role.save(role);

    }

    async findAll() {
        const sql = `SELECT r.id AS roleId,roleName,a.id AS accessId,accessName
        FROM role AS r , role_access AS ra ,access AS a
        WHERE r.id = ra.roleId AND ra.accessId =a.id`
        const roleAccessDto: RoleAccessDto[] = await Role.query(sql);

        const roleAccessDatas: RoleAccessData[] = [];

        const role = await Role.find();
        for (let i = 0; i < role.length; i++) {
            const element = role[i];
            const roleAccessData = new RoleAccessData()
            roleAccessData.id = element.id;
            roleAccessData.roleName = element.roleName;
            for (let j = 0; j < roleAccessDto.length; j++) {
                const item = roleAccessDto[j];
                if (element.id == item.roleId) {
                    roleAccessData.accesses.push({
                        id: item.accessId,
                        accessName: item.accessName
                    });
                }
            }
            roleAccessDatas.push(roleAccessData);
        }

        return roleAccessDatas;
    }

    async remove(id: number) {
        if (id == 3) {
            throw new HttpException({
                message: '此角色为默认角色不让删除',
                error: 'roleName must be unique',
            },
                HttpStatus.BAD_REQUEST);
        }
        await Role.delete(id);
        const role = await Role.findOne({ where: { id: id } });
        if (role) {
            return false;
        }
        return true;
    }

    async update(role: Role) {
        return await Role.save(role);
    }

    async updateRoleAccess(roleAddAccess: RoleAddAccessDto) {

        // return await getManager().transaction(async (entityManager: EntityManager) => {
        const sql = `DELETE FROM role_access WHERE roleId = ?;`
        await RoleAccess.query(sql, [roleAddAccess.roleId]);
        const roleAccesses: RoleAccess[] = [];
        for (let i = 0; i < roleAddAccess.accesses.length; i++) {
            const element = roleAddAccess.accesses[i];
            const roleAccess = new RoleAccess;
            roleAccess.roleId = roleAddAccess.roleId;
            roleAccess.accessId = element.id;
            roleAccesses.push(roleAccess);
        }

        return await RoleAccess.save(roleAccesses);
        // })

    }


}